package com.wv.api.service.basic.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.wv.api.mapper.basic.BasicCommunityDao;
import com.wv.api.service.basic.IBasicCommunityService;
import com.wv.common.result.HttpCodeEnum;
import com.wv.common.result.PageResponseResult;
import com.wv.common.result.Result;
import com.wv.model.basic.BasicCommunity;
import com.wv.model.basic.condition.BasicCommunityCondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


@Service
public class BasicCommunityServiceImpl implements IBasicCommunityService {

    @Autowired
    private BasicCommunityDao basicCommunityDao;

    @Override
    public Result getCommunityList(BasicCommunityCondition basicCommunityCondition) {
        basicCommunityCondition.checkParam();
        PageHelper.startPage(basicCommunityCondition.getPage(), basicCommunityCondition.getSize());
        PageResponseResult build = PageResponseResult.builder()
                .total(basicCommunityDao.selectCommunityCount(basicCommunityCondition))
                .currentPage(basicCommunityCondition.getPage())
                .build();
        build.setData(basicCommunityDao.selectCommunityList(basicCommunityCondition));
        return build;
    }

    @Override
    public Result addCommunity(BasicCommunity basicCommunity) {
        //        生成id
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        basicCommunity.setId(snowflake.nextIdStr());
        basicCommunity.setCreateTime(LocalDateTime.now());
        int i = basicCommunityDao.insert(basicCommunity);
        return i > 0 ? Result.ok() : Result.error(HttpCodeEnum.ADD_ERROR);
    }

    @Override
    public Result updateCommunity(BasicCommunity basicCommunity) {
        int i = basicCommunityDao.updateById(basicCommunity);
        return i > 0 ? Result.ok() : Result.error(HttpCodeEnum.UPDATE_ERROR);
    }

    @Override
    public Result deleteCommunity(String id) {
        int i = basicCommunityDao.deleteById(id);
        return i > 0 ? Result.ok() : Result.error(HttpCodeEnum.DElETE_ERROR);
    }
}
